package cn.wdm.ymcc.mq;

import cn.wdm.ymcc.domain.MessageEmail;
import cn.wdm.ymcc.domain.User;
import cn.wdm.ymcc.feign.UserFeignClient;
import cn.wdm.ymcc.result.JSONResult;
import cn.wdm.ymcc.service.IMessageEmailService;
import cn.wdm.ymcc.utils.SendEmailUtils;
import com.alibaba.fastjson.JSONObject;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.util.List;

@Component
@RocketMQMessageListener(
        consumerGroup = "message-consumer-group"
        ,topic = "message-topic"
        ,selectorExpression = "message-tags")
public class SendEmailFileConsume implements RocketMQListener<MessageExt> {
    @Autowired
    private IMessageEmailService emailService;
    @Autowired
    private UserFeignClient userFeignClient;
    @Override
    public void onMessage(MessageExt messageExt) {
        //1.将消息保存到数据库
        String msgEmailStr = new String(messageExt.getBody(), StandardCharsets.UTF_8);
        MessageEmail msgEmail = JSONObject.parseObject(msgEmailStr, MessageEmail.class);
        emailService.insert(msgEmail);
        //2.发送消息 @TODO 发送短信的幂等没有保证
       /* JSONResult result = userFeignClient.list();
        List<User> users = (List<User>) result.getData();
        System.out.println(users.size());*/
        //SendEmailUtils.sendEmail("1756024408@qq.com"
         //       ,"1756024408@qq.com","上线课程啦","来 快来看");
    }
}
